//
//  PYImageChoiecView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/11.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import ZLPhotoBrowser


class PYImageChoiecView: SYBaseView, UICollectionViewDelegate, UICollectionViewDataSource {

    var collectionView: UICollectionView!
    var lastIndexPath:IndexPath!
    
    var images:[UIImage] = []
    var lastImages:[UIImage] = [] {
        didSet {
            images.append(contentsOf: lastImages)
            self.collectionView.reloadData()
        }
    }
    
    override func initViews() {
        
        self.backgroundColor = .white
        setCollectionView()
        self.addSubview(collectionView)
       
        collectionView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(15.0.autoWidth())
            make.right.equalToSuperview().offset(-15.0.autoWidth())
            make.bottom.equalToSuperview()
        }
        
    }

    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: 100.autoWidth(), height: 100.autoWidth())
        layout.minimumLineSpacing = 20.autoWidth() // 行间距
        layout.minimumInteritemSpacing = 20.autoWidth() 
        layout.scrollDirection = .vertical
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
//        collectionView.d
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(PYImageChoiceCell.self, forCellWithReuseIdentifier: NSStringFromClass(PYImageChoiceCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if images.count == 0 {
            return 1
        } else if images.count < 6 {
            return images.count + 1
        } else {
            return 6
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: PYImageChoiceCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(PYImageChoiceCell.self), for: indexPath) as? PYImageChoiceCell
        if images.count == 0 {
            cell?.pic.image = UIImage(named: "add_image")
            cell?.deletePic.isHidden = true
        } else if images.count < 6 {
            
            if indexPath.row == images.count {
                cell?.pic.image = UIImage(named: "add_image")
                cell?.deletePic.isHidden = true
            } else {
                cell?.pic.image = images[indexPath.row]
                cell?.deletePic.isHidden = false
            }
        } else {
            cell?.pic.image = images[indexPath.row]
            cell?.deletePic.isHidden = false
        }
        cell?.clickedClosure = { index in
            self.images.remove(at: indexPath.row)
            self.collectionView.reloadData()
        }
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let vc = self.currentVC as! PYEditViewController
        vc.textView.resignFirstResponder()
        
        if images.count == 0 {

            (self.currentVC as! PYEditViewController).choiceView.isHidden = false
        } else if images.count < 6 {
            if indexPath.row == images.count {
                (self.currentVC as! PYEditViewController).choiceView.isHidden = false
            } else {
//                let vc = ZLPhotoPreviewController(photos: self.arrDataSources, index: index)
                
            }
        } else {
            
        }
    }
    
    
    
    
}


//
//  DYYuyueTimeSubCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import YYText
import Kingfisher

class PYImageChoiceCell: SYBaseCollectionViewCell {
    
    weak var delegate: PYTableViewCellDelegate?
    
    //当前cell索引
    var cellIndexPath: IndexPath?
    
    let pic: AnimatedImageView = {
        let v: AnimatedImageView = AnimatedImageView()
        v.image = UIImage(named: "add_image")
        
        return v
    }()
    
    let deletePic: UIButton = {
        let v: UIButton  = UIButton()
        v.setImage(UIImage(named: "delete_image"), for: .normal)
        v.layer.cornerRadius = 6
        return v
    }()

    public func configureView() {
        
    }
    
    override func initViews() {
        
        deletePic.addTarget(self, action: #selector(deleteAction), for: .touchUpInside)
        deletePic.isHidden = true
        
        let tap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapPicture(tap:)))
        pic.addGestureRecognizer(tap)
        
        addSubview(pic)
        addSubview(deletePic)
        deletePic.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.height.width.equalTo(15.0.autoWidth())
            
        }
        pic.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    @objc func deleteAction() {
        
        if let c = clickedClosure {
            c(0)
        }
    }
    
    @objc func tapPicture(tap: UITapGestureRecognizer) {
        
//            let animationView: AnimatedImageView = tap.view as! AnimatedImageView
//            if (self.delegate?.responds(to: #selector(PYTableViewCellDelegate.tableViewCell(_:tapImageAction:indexPath:))))! {
//                self.delegate?.tableViewCell(self, tapImageAction: animationView.tag, indexPath: self.cellIndexPath!)
//            }
    }
}

